// 
//  downloadmanager.js
//  firefox
//  
//  Created by Zak on 2008-06-17.
//  Copyright 2008 Ant.com. All rights reserved.
// 

top.AntDownloadManager = 
{
	/**
	 * start
	 */
	init: function ()
	{
	},
	
	/**
	 * stop 
	 */
	destroy: function ()
	{
	},

	/**
	 * return the movies destination folder and create it if it doesn't exist
	 */
	getDestFolder: function () 
	{
		var pref = AntPrefs.getAntBranch();
        var flvdir;
		var file = CCIN("@mozilla.org/file/local;1", "nsILocalFile");
        
        try
        {
            flvdir = pref.getCharPref("flvdir");
            file.initWithPath(flvdir);
            if (!file.exists()) 
                file.create(CI("nsIFile").DIRECTORY_TYPE, 0777);
            if (!file.isDirectory())
            {
                alert(AntLang.getString("AntDownloadManager.InvalidDestinationFolder"));
                return null;
            }
        }
        catch (e) { alert(AntLang.getString("AntDownloadManager.NoFlvDir")); }

		return file;
	},

	/**
	 * public function to download a flv from the toolbar
	 * @param origin the origin web site where the movie is from ex youtube
	 * @param url where the flv is
	 * @param name of the video
	 * @param doNotOpen if true the function won't open the firefox download manager
	 * @return destFile.path if available else false
	 */
	downloadFlv: function (flvLink, doNotOpen)
	{
		flvLink.name = flvLink.name.sanitize();
		flvLink.origin = flvLink.origin.sanitize();
		
		var destFile = this.getDestFolder();
		if (!destFile)
			return false;

		destFile.append(flvLink.origin + "." + flvLink.name + ".flv");
		while (destFile.exists())
		{
			destFile = this.getDestFolder();
			flvLink.incrementFlvName();
			destFile.append(flvLink.origin + "." + flvLink.name + ".flv");
		}
		destFile.create(CI("nsIFile").NORMAL_FILE_TYPE, 0777);
		this.download(flvLink, destFile);
		if (!doNotOpen)
			this.openDownloadWindow();
		return destFile.path;
	},

	/**
	 * openDownloadWindow opens the firefox downloadManager
	 */
	openDownloadWindow: function ()
	{
        AntLib.openWindow("Download:Manager", "chrome://mozapps/content/downloads/downloads.xul", {});
	},

	/**
	 * low level function to download a flv
	 * @param flvlink AntFlvLink object
	 * @param dest destination file type nsILocalFile
	 */
	download: function (flvlink, dest)
	{
		try
		{
			var s_uri = ioService.newURI(flvlink.url, null, null);
			var t_uri = ioService.newFileURI(dest);
			var nsIWBP = CI("nsIWebBrowserPersist");
            const browserPersist = CCIN('@mozilla.org/embedding/browser/nsWebBrowserPersist;1', "nsIWebBrowserPersist");

			browserPersist.persistFlags =	nsIWBP.PERSIST_FLAGS_NO_CONVERSION | 
						                    nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
//											nsIWBP.PERSIST_FLAGS_FROM_CACHE |
											nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
											nsIWBP.PERSIST_FLAGS_CLEANUP_ON_FAILURE;

			var ret = dlMgr.addDownload(0, s_uri, t_uri, flvlink.name.replace(/_/g, " "), null, null, null, null, browserPersist);

            AntPrefs.flvToPlay(dest.path);
			browserPersist.progressListener = ret;
            // AntLib.toLog("flvlink.header = " + flvlink.header);
			browserPersist.saveURI(s_uri, null, null, null, flvlink.header, t_uri);
		}
		catch (e)
		{
			alert(AntLang.getString("AntDownloadManager.DownloadError"));
			AntLib.toLog("AntDownloadManager: Download error: " + e);
		}
	},
}
